
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ESP32 音频播放器2</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root {
            --primary: #4361ee;
            --secondary: #3a0ca3;
            --success: #4cc9f0;
            --danger: #f72585;
            --warning: #fca311;
            --light: #f8f9fa;
            --dark: #212529;
            --gray: #6c757d;
            --card-bg: #ffffff;
            --body-bg: #f0f2f5;
            --border-radius: 12px;
            --box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
            --transition: all 0.3s ease;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: var(--body-bg);
            color: var(--dark);
            line-height: 1.6;
            padding: 20px 15px;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        .container {
            width: 100%;
            max-width: 600px;
            margin: 0 auto;
        }

        header {
            text-align: center;
            margin-bottom: 30px;
            width: 100%;
        }

        h1 {
            color: var(--primary);
            font-size: 2.5rem;
            margin-bottom: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 15px;
        }

        h2 {
            color: var(--secondary);
            font-size: 1.5rem;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .card {
            background: var(--card-bg);
            border-radius: var(--border-radius);
            box-shadow: var(--box-shadow);
            padding: 25px;
            margin-bottom: 25px;
            transition: var(--transition);
        }

        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 25px rgba(0, 0, 0, 0.12);
        }

        .status {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            padding: 8px 16px;
            background: rgba(76, 201, 240, 0.15);
            border-radius: 50px;
            font-weight: 600;
            margin-bottom: 20px;
        }

        .status.playing {
            color: #0a9396;
        }

        .status.paused {
            color: var(--warning);
        }

        .status.stopped {
            color: var(--danger);
        }

        .custom-select {
            position: relative;
            margin-bottom: 20px;
        }

        .select-btn {
            width: 100%;
            padding: 14px 20px;
            background: white;
            border: 1px solid #ddd;
            border-radius: var(--border-radius);
            text-align: left;
            font-size: 16px;
            cursor: pointer;
            display: flex;
            justify-content: space-between;
            align-items: center;
            transition: var(--transition);
        }

        .select-btn:hover {
            border-color: var(--primary);
        }

        .options-container {
            position: absolute;
            top: 100%;
            left: 0;
            width: 100%;
            max-height: 300px;
            overflow-y: auto;
            background: white;
            border-radius: var(--border-radius);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            z-index: 100;
            display: none;
            margin-top: 5px;
        }

        .option-item {
            padding: 12px 20px;
            cursor: pointer;
            transition: var(--transition);
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .option-item:hover {
            background: #f0f5ff;
        }

        .option-item i {
            color: var(--primary);
        }

        .url-input {
            width: 100%;
            padding: 14px 20px;
            border: 1px solid #ddd;
            border-radius: var(--border-radius);
            font-size: 16px;
            margin-bottom: 15px;
            transition: var(--transition);
        }

        .url-input:focus {
            border-color: var(--primary);
            outline: none;
            box-shadow: 0 0 0 3px rgba(67, 97, 238, 0.2);
        }

        .btn {
            padding: 12px 24px;
            border: none;
            border-radius: var(--border-radius);
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: var(--transition);
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
        }

        .btn-primary {
            background: var(--primary);
            color: white;
        }

        .btn-primary:hover {
            background: #3651d4;
            transform: translateY(-2px);
        }

        .btn-danger {
            background: var(--danger);
            color: white;
        }

        .btn-danger:hover {
            background: #e3126f;
            transform: translateY(-2px);
        }

        .btn-success {
            background: var(--success);
            color: white;
        }

        .btn-success:hover {
            background: #3ab7d9;
            transform: translateY(-2px);
        }

        .slider-container {
            margin: 20px 0;
        }

        .slider {
            -webkit-appearance: none;
            width: 100%;
            height: 10px;
            border-radius: 5px;
            background: #e9ecef;
            outline: none;
        }

        .slider::-webkit-slider-thumb {
            -webkit-appearance: none;
            appearance: none;
            width: 22px;
            height: 22px;
            border-radius: 50%;
            background: var(--primary);
            cursor: pointer;
            transition: var(--transition);
        }

        .slider::-webkit-slider-thumb:hover {
            background: var(--secondary);
            transform: scale(1.1);
        }

        .value-display {
            font-weight: 600;
            color: var(--primary);
            font-size: 1.2rem;
            margin-left: 5px;
        }

        .control-group {
            display: flex;
            gap: 15px;
            margin-top: 20px;
            flex-wrap: wrap;
        }

        .control-group .btn {
            flex: 1;
            min-width: 140px;
        }

        footer {
            margin-top: 30px;
            text-align: center;
            color: var(--gray);
            font-size: 0.9rem;
        }

        @media (max-width: 576px) {
            h1 {
                font-size: 2rem;
            }
            
            .card {
                padding: 20px;
            }
            
            .control-group {
                flex-direction: column;
            }
            
            .control-group .btn {
                width: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1><i class='fas fa-music'></i> ESP32 音频播放器</h1>
            <p>远程控制您的ESP32音频播放设备</p>
        </header>

        <!-- 播放控制 -->
        <div class="card">
            <h2><i class="fas fa-play-circle"></i> 播放控制</h2>
            <div class="status playing" id="playing">
                {status}
            </div>
            
            <div class="custom-select">
                <button class="select-btn" id="customSelectBtn">
                    <span>怀集音乐之声</span>
                    <i class="fas fa-chevron-down"></i>
                </button>
                <div class="options-container" id="customOptions">
                    <!-- 动态生成电台列表 -->
                </div>
            </div>
            
            <input type="text" class="url-input" id="url" placeholder="输入新的MP3链接" value="http://lhttp.qingting.fm/live/4804/64k.mp3">
            
            <button class="btn btn-primary" onclick="changeUrl()"><i class="fas fa-exchange-alt"></i> 更换音频</button>
            <button class="btn btn-danger" onclick="stopPlayback()"><i class="fas fa-stop-circle"></i> 停止播放</button>
        </div>

        <!-- 音量控制 -->
        <div class="card">
            <h2><i class="fas fa-volume-up"></i> 音量控制</h2>
            <div class="slider-container">
                <p>当前音量: <span class="value-display" id="volDisplay">{volume}</span>/21</p>
                <input type="range" class="slider" id="volume" min="0" max="21" value="{volume}" oninput="updateVol(this.value)">
            </div>
            <button class="btn btn-primary" onclick="setVolume()">
                <i class="fas fa-sliders-h"></i> 设置音量
            </button>
        </div>

        <!-- 屏幕与亮度控制 -->
        <div class="card">
            <h2><i class="fas fa-desktop"></i> 屏幕亮度控制</h2>
            <div class="slider-container">
                <p>亮度: <span class="value-display" id="brightDisplay">{lastBrightness}</span>%</p>
                <input type="range" class="slider" id="brightness" min="0" max="100" value="{lastBrightness}" oninput="updateBright(this.value)">
            </div>
            
            <div class="control-group">
                <button class="btn btn-primary" onclick="setBrightness()">
                    <i class="fas fa-sun"></i> 设置亮度
                </button>
                <button class="btn btn-danger" onclick="screenOff()">
                    <i class="fas fa-power-off"></i> 关闭屏幕
                </button>
                <button class="btn btn-success" onclick="screenOn()">
                    <i class="fas fa-play"></i> 开启屏幕
                </button>
            </div>
        </div>
		<!-- 配置按钮 -->
		<div class="card">
			<h2><i class="fas fa-cog"></i> 系统配置</h2>
			<button class="btn btn-primary" onclick="location.href='/config'">
				<i class="fas fa-cogs"></i> 打开配置
			</button>
		</div>
        
        <footer>
            <p>ESP32音频播放器控制界面 &copy;by简 2025</p>
        </footer>
    </div>

    <script type="text/javascript">
        const JSON_URL = 'https://fm-73y.pages.dev/list.json';
        const selectBtn = document.getElementById('customSelectBtn');
        const optionsList = document.getElementById('customOptions');
        const urlInput = document.getElementById('url');

        function jzdatasj() {
            fetch('/getJsonUrl')
                .then(r => r.text())
                .then(url => {
                    return fetch(url.trim());
                })
                .then(r => r.json())
                .then(data => {
                    var MP3dataList = data;
                    optionsList.innerHTML = '';
                    MP3dataList.forEach(station => {
                        const div = document.createElement('div');
                        div.className = 'option-item';
                        div.setAttribute('data-url', station.url);
                        div.innerHTML = `<i class="fas fa-music"></i><span>${station.title}</span>`;
                        div.addEventListener('click', () => {
                            selectBtn.querySelector('span').textContent = station.title;
                            urlInput.value = station.url;
                            optionsList.style.display = 'none';
                            selectBtn.querySelector('i.fas').classList.replace('fa-chevron-up', 'fa-chevron-down');
                        });
                        optionsList.appendChild(div);
                    });
                })
                .catch(err => {
                    console.error('加载电台列表失败:', err);
                    alert('无法加载电台列表，请检查网络或播放源设置。');
                });
        }

        jzdatasj(); 




        // 点击按钮切换下拉框显示
        selectBtn.addEventListener('click', (e) => {
            e.stopPropagation();
            optionsList.style.display = optionsList.style.display === 'block' ? 'none' : 'block';
            selectBtn.querySelector('i').classList.toggle('fa-chevron-down');
            selectBtn.querySelector('i').classList.toggle('fa-chevron-up');
        });

        // 点击其他区域隐藏下拉框
        document.addEventListener('click', (e) => {
            if (!selectBtn.contains(e.target) && !optionsList.contains(e.target)) {
                optionsList.style.display = 'none';
                selectBtn.querySelector('i').classList.add('fa-chevron-down');
                selectBtn.querySelector('i').classList.remove('fa-chevron-up');
            }
        });

        // 点击选项赋值并隐藏下拉框
        const optionItems = document.querySelectorAll('.option-item');
        optionItems.forEach(item => {
            item.addEventListener('click', () => {
                const text = item.querySelector('span').textContent;
                const url = item.getAttribute('data-url');
                
                // 更新按钮显示和URL输入框
                selectBtn.querySelector('span').textContent = text;
                urlInput.value = url;
                
                // 隐藏下拉框
                optionsList.style.display = 'none';
                selectBtn.querySelector('i').classList.add('fa-chevron-down');
                selectBtn.querySelector('i').classList.remove('fa-chevron-up');
            });
        });

        // 功能函数
        function changeUrl() {
            let url = document.getElementById('url').value;
            fetch('/play', {
                method: 'POST',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                body: 'url=' + encodeURIComponent(url)
            }).then(r => r.text()).then(t => {
                document.getElementById('playing').innerHTML = '<i class="fas fa-circle-play"></i><span>正在播放</span>';
                alert('已发送播放指令: ' + url);
            });
        }

        function updateVol(val) {
            document.getElementById('volDisplay').innerText = val;
        }

        function setVolume() {
            let vol = document.getElementById('volume').value;
            fetch('/setVolume', {
                method: 'POST',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                body: 'vol=' + vol
            }).then(r => r.text()).then(t => {
                alert('音量已设置为: ' + vol);
            });
        }

        function updateBright(val) {
            document.getElementById('brightDisplay').innerText = val;
        }

        function setBrightness() {
            let bright = document.getElementById('brightness').value;
            fetch('/setBrightness', {
                method: 'POST',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                body: 'brightness=' + bright
            }).then(r => r.text()).then(t => {
                alert('亮度已设置为: ' + bright + '%');
            });
        }

        function screenOff() {
            fetch('/setBrightness', {
                method: 'POST',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                body: 'brightness=0'
            }).then(r => r.text()).then(t => {
                document.getElementById('brightness').value = 0;
                document.getElementById('brightDisplay').innerText = 0;
                alert('屏幕已关闭');
            });
        }

        function screenOn() {
            let bright = 30;
            fetch('/setBrightness', {
                method: 'POST',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                body: 'brightness=' + bright
            }).then(r => r.text()).then(t => {
                document.getElementById('brightness').value = bright;
                document.getElementById('brightDisplay').innerText = bright;
                alert('屏幕已开启，亮度恢复为 ' + bright + '%');
            });
        }
        //关闭音乐
        function stopPlayback() {
            fetch('/stopPlay', {
                method: 'GET'
            })
            .then(r => r.text())
            .then(t => {
                document.getElementById('playing').innerHTML = '<i class="fas fa-stop-circle"></i><span>已停止</span>';
                alert('播放已停止');
            })
            .catch(err => {
                alert('停止失败: ' + err);
            });
        }
    </script>
</body>
</html>